package rabbit.topic;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @Author: admin
 * @Description:
 * @Date: 2018/7/31 14:56
 */


public class TestConsumer {
    private static final String EXCHANGE_NAME = "testexchange";// "outerhostexchange";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        //factory.setHost("39.106.17.245");
        //factory.setHost("39.106.144.161");
        factory.setHost("192.168.10.66");//"03156721261"
        factory.setVirtualHost("OuterHost");
        factory.setUsername("root");
        factory.setPassword("root");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC, true);
        String queueName = "testqueue";//"housequeue";
        channel.queueBind(queueName, EXCHANGE_NAME, "testtopic");//"houseTopic");
        channel.basicQos(100);

        //channel.ba
        //channel.basicAck(envelope.getDeliveryTag(),false);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println(message);
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                channel.basicNack();
                channel.basicAck(envelope.getDeliveryTag(), false);
                //channel.basicNack(envelope.getDeliveryTag(),false,false);
                //channel.basicReject(envelope.getDeliveryTag(),false);
            }
        };
        channel.basicConsume(queueName, false, consumer);
        //channel.basicConsume(queueName, true, consumer);
    }
}